// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";

import MuseUI from "muse-ui";
import "muse-ui/dist/muse-ui.css";
import "muse-ui/dist/theme-teal.css";

import App from "./App";
import router from "./router";

Vue.use(MuseUI);
Vue.config.productionTip = false;

const _Promise = window.Promise;
window.Promise = function Promise(...args) {
  const res_promise = new _Promise(...args);
  res_promise.catch(function(err) {
    Raven.captureException(err, {
      level: "error",
      logger: "Promise"
    });
  });
  return res_promise;
};
["resolve", "reject", "all", "race"].forEach(function(key) {
  Promise[key] = _Promise[key];
});
window.addEventListener("error", err => {
  Raven.captureException(err, {
    level: "error",
    logger: "window.onerror"
  });
});
window.addEventListener("unhandledrejection", err => {
  Raven.captureException(err, {
    level: "error",
    logger: "window.unhandledrejection"
  });
});
Vue.config.errorHandler = function(err, vm) {
  Raven.captureException(err, {
    level: "error",
    logger: "Vue.config.errorHandler",
    tags: { vm_name: vm ? vm.name : "???" }
  });
};

const query = (window.query = {});
location.search.substr(1).split("&").forEach(query_str => {
  const query_info = query_str.split("=");
  query[query_info[0]] = decodeURIComponent(query_info[1]);
});

window.base_href = (() => {
  var bases = document.getElementsByTagName("base");
  var baseHref = null;

  if (bases.length > 0) {
    baseHref = bases[0].href;
  } else {
    baseHref = `http://${location.host}/`;
  }
  return baseHref;
})();
window.server_href =
  window.__server_href__ || `http://${location.hostname}:3000`;
window.resource_href =
  window.__resource_href__ || `http://${location.hostname}:8081/`;

// 连接到服务器
window.socket = io(server_href);
window.socket_promise_args = {};
window.socket_promise = new Promise((resolve, reject) => {
  socket_promise_args.resolve = resolve;
  socket_promise_args.reject = reject;
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  template: "<App/>",
  components: {
    App
  }
});
